package com.example.easyexceldemo.entity;

import java.time.YearMonth;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
 
public class DateValidator {
    public static boolean isValidDate(int year, int month, int day) {
        try {
            LocalDate date = LocalDate.of(year, month, day);
            // 验证年份和月份
            if (date.getYear() != year || date.getMonth().getValue() != month) {
                return false;
            }
            // 验证天数是否在月份之内
            YearMonth yearMonth = YearMonth.of(year, month);
            long daysInMonth = ChronoUnit.DAYS.between(yearMonth.atDay(1), yearMonth.atEndOfMonth().plusDays(1));
            return day >= 1 && day <= daysInMonth;
        } catch (Exception e) {
            return false;
        }
    }
 
    public static void main(String[] args) {
        int year = 2023;
        int month = 2;
        int day = 31; // 非闰年中不能是29
        System.out.println("Is valid date: " + isValidDate(year, month, day));
        String ddd = "2024/01/22";
        String[] split = ddd.split("/");
        System.out.println(split[0]);
        System.out.println(split[1]);
        System.out.println(split[2]);
    }
}
